const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { optimization } = require("../app1/webpack.dev");
// const { ModuleFederationPlugin } = require('webpack').container;
const { ModuleFederationPlugin } = require('@module-federation/enhanced/webpack');
const packageName = require('./package.json').name;

module.exports = {
  mode: "development",
  entry: "./index.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    library: `${packageName}-[name]`,
    libraryTarget: 'umd',
    chunkLoadingGlobal: `webpackJsonp_${packageName}`,
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-react"],
        },
      },
    ],
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      title: "App1",
      filename: "index.html",
    }),
    new ModuleFederationPlugin({
      name: 'app2',
      filename: 'app2.js',
      remotes: {
        app1: "app1@http://localhost:8888/app1.js",
      },
      exposes: {
        "./App": "./App.js",
        './Button': './Button.jsx',
      },
      shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true } },
    }),
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, "public"),
    },
    compress: true,
    port: 9999,
    host: "localhost",
    headers: {
      'Access-Control-Allow-Origin': 'http://localhost:3000'
    }
  },
  resolve: {
    extensions: ['.jsx', '.js']
  }
    // stats: 'minimal'
};
